library(tidyverse)
library(car)

## set working directory to 
## Dataverse replication file

survey <- read.table("Harris_Data/Harris 1978 Political Issues and Congressional Election Survey, study no. P3849/harris_p3849_spss.tab", header = TRUE)

# pid
survey$pid <- c(1:nrow(survey))

# study 
survey$study <- as.character(3849)

# study year (year)
survey$year <- 1978

# geographic data (urban)
survey$urban <- NA

# geographic data (region)
survey$region <- NA

# respondent head of household (hh)
survey$hh <- NA

# increasing inequality (inequality)
table(survey$Q7_2)
survey$inequality <- car::recode(survey$Q7_2, 
                            "1 = 'Feel'; 
                             2 = 'Don t feel'; 
                             3 = 'Not sure'")
table(survey$inequality)

# inequality variable (inequality.variable)
survey$inequality.variable <- 1

# union (union.self) union other 
table(survey$F5_2)
survey$union.self <-   car::recode(survey$F5_1, "0 = 'No'; 1 = 'Yes'; else = NA")
survey$union.other <- car::recode(survey$F5_2, "0 = 'No'; 1 = 'Yes'; else = NA")  

table(survey$union.self[survey$F5_4 == 1])
table(survey$union.other[survey$F5_4 == 1])

survey$union.self[survey$F5_4 == 1] <- "Not Sure"
survey$union.other[survey$F5_4 == 1] <- "Not Sure"


table(survey$union.other)
table(survey$union.self)


# employment (employed)
survey$employed <- dplyr::recode(survey$F1A,
                                        `1` = "Hourly wage worker",
                                        `2` = "Salaried",
                                        `3` = "Self-employed",
                                        `4` = "Retired",
                                        `5` = "Unemployed",
                                        `6` = "Student",
                                        `7` = "Military service",
                                        `8` = "Housewife",
                                        `9` = "Disabled",
                                        `10` = "Other (SPECIFY)")
table(survey$employed)

# empl self
survey$employed.self <- NA

# occupation
survey$occupation <- as.character(dplyr::recode(survey$F1B,
                                          `1` = "Professional",
                                          `2` = "Manager, official",
                                          `3` = "Proprietor (small business)",
                                          `4` = "Clerical worker",
                                          `5` = "Sales worker",
                                          `6` = "Skilled craftsman, foreman",
                                          `7` = "Operative, unskilled laborer (except farm)",
                                          `8` = "Service worker",
                                          `9` = "Farmer, farm manager, farm laborer",
                                          `10` = "Other (SPECIFY)"))
table(survey$occupation)

# occ self
survey$occupation.self <- NA

# household size (hhsize)
survey$hhsize <- NA

# education (educ)
survey$educ <- as.character(survey$F4)
table(survey$F4)
survey$educ <- car::recode(survey$educ, 
                          "1 = 'No formal schooling';
                           2 = '1-7 years completed';
                           3 = '8 years completed';
                           4 = 'Some high school';
                           5 = 'High school graduate';
                           6 = 'Some college';
                           7 = 'Two year college graduate';
                           8 = 'Four year college graduate';
                           9 = 'Post graduate';
                           else = 'NA'")
table(survey$educ)                        

# household income (income)
survey$income <- as.character(dplyr::recode(survey$F6,
                                      `1` = "Under $5,000",
                                      `2` = "$5,000 to $6,999",
                                      `3` = "$7,000 to $9,999",
                                      `4` = "$10,000 to $14,999",
                                      `5` = "$15,000 to $19,999",
                                      `6` = "$20,000 to $24,999",
                                      `7` = "$25,000 to $34,999",
                                      `8` = "$35,000 and over",
                                      `9` = "Not sure/refused"))
table(survey$income)

# age
survey$age <- as.character(dplyr::recode(survey$F3,
                                   `1` = "18 to 20",
                                   `2` = "21 to 24",
                                   `3` = "25 to 29",
                                   `4` = "30 to 34",
                                   `5` = "35 to 39",
                                   `6` = "40 to 49",
                                   `7` = "50 to 64",
                                   `8` = "65 and over",
                                   `9` = "Refused"))
table(survey$age)

# race
table(survey$F8)
survey$race <- as.character(dplyr::recode(survey$F8,
                                    `1` = "White",
                                    `2` = "Black",
                                    `3` = "Oriental",
                                    `4` = "Spanish-American (Puerto Rican, Mexican-American, etc.)",
                                    `5` = "Other (SPECIFY)",
                                    `6` = "Refused"))
table(survey$race)
#survey$race[survey$race == "Refused"] <- NA
table(survey$race)

# politics (party)
survey$party <- as.character(dplyr::recode(survey$Q1E,
                                     `1` = "Republican",
                                     `2` = "Democrat",
                                     `3` = "Independent",
                                     `4` = "Other",
                                     `5` = "Not sure"))
table(survey$party)

# politics (ideology)
survey$ideology <- as.character(dplyr::recode(survey$Q1H,
                                        `1` = "Conservative",
                                        `2` = "Middle-of-the-road",
                                        `3` = "Liberal",
                                        `4` = "Radical",
                                        `5` = "Not sure"))
table(survey$ideology)

# gender
survey$gender <- as.character(dplyr::recode(survey$F9,
                                      `1` = "Male",
                                      `2` = "Female"))
table(survey$gender)

# religion
survey$religion <- as.character(dplyr::recode(survey$F7,
                                        `1` = "Protestant",
                                        `2` = "Catholic",
                                        `3` = "Jewish",
                                        `4` = "Other (SPECIFY)",
                                        `5` = "None",
                                        `6` = "Not sure"))
table(survey$religion)

# factuals
survey$factual1 <- NA
survey$factual2 <- NA
survey$factual3 <- NA

## alienation index
survey$dontcare <- car::recode(survey$Q7_1, 
                                 "1 = 'Feel'; 
                                 2 = 'Don t feel'; 
                                 3 = 'Not sure'")
survey$dontcount <- car::recode(survey$Q7_4, 
                                  "1 = 'Feel'; 
                                  2 = 'Don t feel'; 
                                  3 = 'Not sure'")
survey$leftout <- car::recode(survey$Q7_5, 
                                "1 = 'Feel'; 
                                2 = 'Don t feel'; 
                                3 = 'Not sure'")

## question placement
survey$question_place <- "after party"

# subset
survey_3849 <- survey[,c("pid", "study", "year", "urban", "region", "hh",
                         "inequality", "inequality.variable", "union.self", "union.other",
                         "employed", "employed.self", "occupation", "occupation.self", "hhsize", "educ", "income", 
                         "age", "race", "party", "ideology", "gender", "religion",
                         "factual1", "factual2", "factual3", "dontcare", "dontcount", "leftout",
                         "question_place")]


# save file
#saveRDS(survey_3849, file = "Harris_Data/survey_3849.rds")
